<?php

namespace app\common\library;
class Utils
{
    /**
     * @param int $ts
     * @param int $mode
     * @return false|string
     */
    static public function date($ts, $mode = 1)
    {
        if ($ts === 0) return '';
        $mode = (int)$mode;
        switch ($mode) {
            case 1:
                return \date('Y-m-d H:i:s', $ts);
            case 2:
                return \date('Y/m/d H:i:s', $ts);

            default:
                return \date('Y-m-d H:i:s', $ts);


        }
    }

    /**
     * @param $date int
     * @return string
     */
    static public function dateToTime($date)
    {
        $year = $date / 10000;
        $month = $date % 10000 / 100;
        $day = $date % 100;
        $str = sprintf('%4d-%02d-%02d 00:00:00', $year, $month, $day);
        return strtotime($str);
    }

    /**
     * @param $data
     * @return string
     */
    static public function base64url_encode($data)
    {
        return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
    }

    /**
     * @param $data string
     * @return bool|string
     */
    static public function base64url_decode($data)
    {
        return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
    }

}
